/*
 * File: foc.c
 *
 * Code generated for Simulink model 'foc'.
 *
 * Model version                  : 1.9
 * Simulink Coder version         : 23.2 (R2023b) 01-Aug-2023
 * C/C++ source code generated on : Wed Dec  6 11:37:47 2023
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: STMicroelectronics->ST10/Super10
 * Code generation objectives:
 *    1. Execution efficiency
 *    2. RAM efficiency
 * Validation result: Not run
 */

#include "foc.h"
#include <math.h>
#include "rtwtypes.h"

/* External inputs (root inport signals with default storage) */
ExtU rtU;

/* External outputs (root outports fed by signals with default storage) */
ExtY rtY;

/* Real-time model */
static RT_MODEL rtM_;
RT_MODEL *const rtM = &rtM_;

/* Model step function */
void foc_step(void)
{
  real_T rtb_Sum4;
  real_T rtb_Switch_idx_0;
  real_T rtb_add_b;
  uint16_T rtb_Get_Integer_tmp_tmp;

  /* If: '<S8>/If' incorporates:
   *  Constant: '<S9>/Constant'
   *  DataTypeConversion: '<S10>/Convert_back'
   *  DataTypeConversion: '<S10>/Convert_uint16'
   *  DataTypeConversion: '<S11>/Convert_back'
   *  DataTypeConversion: '<S11>/Convert_uint16'
   *  Gain: '<S5>/indexing'
   *  Inport: '<Root>/REG'
   *  RelationalOperator: '<S9>/Compare'
   *  Sum: '<S10>/Sum'
   *  Sum: '<S11>/Sum'
   */
  if (rtU.reg < 0.0) {
    /* Outputs for IfAction SubSystem: '<S8>/If Action Subsystem' incorporates:
     *  ActionPort: '<S10>/Action Port'
     */
    rtb_add_b = rtU.reg - (real_T)(int16_T)floor(rtU.reg);

    /* End of Outputs for SubSystem: '<S8>/If Action Subsystem' */
  } else {
    /* Outputs for IfAction SubSystem: '<S8>/If Action Subsystem1' incorporates:
     *  ActionPort: '<S11>/Action Port'
     */
    rtb_add_b = rtU.reg - (real_T)(int16_T)rtU.reg;

    /* End of Outputs for SubSystem: '<S8>/If Action Subsystem1' */
  }

  rtb_add_b *= 800.0;

  /* End of If: '<S8>/If' */

  /* DataTypeConversion: '<S5>/Get_Integer' */
  rtb_Get_Integer_tmp_tmp = (uint16_T)rtb_add_b;

  /* Sum: '<S5>/Sum2' incorporates:
   *  DataTypeConversion: '<S5>/Data Type Conversion1'
   *  DataTypeConversion: '<S5>/Get_Integer'
   */
  rtb_add_b -= (real_T)(uint16_T)rtb_add_b;

  /* Selector: '<S5>/Lookup' incorporates:
   *  Constant: '<S5>/sine_table_values'
   *  DataTypeConversion: '<S5>/Get_Integer'
   *  Sum: '<S7>/Sum3'
   */
  rtb_Sum4 = rtConstP.sine_table_values_Value[(int16_T)rtb_Get_Integer_tmp_tmp];

  /* Sum: '<S7>/Sum4' incorporates:
   *  Constant: '<S5>/offset'
   *  Constant: '<S5>/sine_table_values'
   *  DataTypeConversion: '<S5>/Get_Integer'
   *  Product: '<S7>/Product'
   *  Selector: '<S5>/Lookup'
   *  Sum: '<S5>/Sum'
   *  Sum: '<S7>/Sum3'
   */
  rtb_Sum4 += (rtConstP.sine_table_values_Value[(int16_T)
               (rtb_Get_Integer_tmp_tmp + 1UL)] - rtb_Sum4) * rtb_add_b;

  /* Selector: '<S5>/Lookup' incorporates:
   *  Constant: '<S5>/offset'
   *  Constant: '<S5>/sine_table_values'
   *  DataTypeConversion: '<S5>/Get_Integer'
   *  Sum: '<S5>/Sum'
   *  Sum: '<S7>/Sum5'
   */
  rtb_Switch_idx_0 = rtConstP.sine_table_values_Value[(int16_T)
    (rtb_Get_Integer_tmp_tmp + 200UL)];

  /* Sum: '<S7>/Sum6' incorporates:
   *  Constant: '<S5>/offset'
   *  Constant: '<S5>/sine_table_values'
   *  DataTypeConversion: '<S5>/Get_Integer'
   *  Product: '<S7>/Product1'
   *  Selector: '<S5>/Lookup'
   *  Sum: '<S5>/Sum'
   *  Sum: '<S7>/Sum5'
   */
  rtb_add_b = (rtConstP.sine_table_values_Value[(int16_T)
               (rtb_Get_Integer_tmp_tmp + 201UL)] - rtb_Switch_idx_0) *
    rtb_add_b + rtb_Switch_idx_0;

  /* Outputs for Atomic SubSystem: '<S3>/Two inputs CRL' */
  /* Switch: '<S12>/Switch' incorporates:
   *  Inport: '<Root>/LD'
   *  Inport: '<Root>/LQ'
   *  Product: '<S6>/dcos'
   *  Product: '<S6>/dsin'
   *  Product: '<S6>/qcos'
   *  Product: '<S6>/qsin'
   *  Sum: '<S6>/sum_alpha'
   *  Sum: '<S6>/sum_beta'
   */
  rtb_Switch_idx_0 = rtU.d * rtb_add_b - rtU.q * rtb_Sum4;
  rtb_Sum4 = rtU.q * rtb_add_b + rtU.d * rtb_Sum4;

  /* Gain: '<S4>/one_by_two' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S6>/a16'
   */
  rtb_add_b = 0.5 * rtb_Switch_idx_0;

  /* Gain: '<S4>/sqrt3_by_two' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S6>/a16'
   */
  rtb_Sum4 *= 0.8660254037844386;

  /* Outport: '<Root>/UV' incorporates:
   *  AlgorithmDescriptorDelegate generated from: '<S6>/a16'
   *  Sum: '<S4>/add_b'
   *  Sum: '<S4>/add_c'
   */
  rtY.UV[0] = rtb_Switch_idx_0;

  /* End of Outputs for SubSystem: '<S3>/Two inputs CRL' */
  rtY.UV[1] = rtb_Sum4 - rtb_add_b;
  rtY.UV[2] = (0.0 - rtb_add_b) - rtb_Sum4;
}

/* Model initialize function */
void foc_initialize(void)
{
  /* (no initialization code required) */
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */
